﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

public partial class system_zmag_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TUser.CheckUser(this);
            string ecrm_email = this.Session["ecrm_email"].ToString();
            if (TCommon.postusers.IndexOf(ecrm_email) != -1)
            {
            }
            else
            {
                Response.Write("您未得到此项功能的授权！");
                Response.End();
            }
        }
    }
    protected void btnRun_Click(object sender, EventArgs e)
    {
        string result = "";
        result += "<table id=\"mymans\" border=\"0\" cellpadding=\"1\" cellspacing=\"2\" align=\"left\" width=100%>";
        result += "<thead>";
        result += "<tr style=\"background-color:#f5f5f5;\">";
        result += "<td align=center>序号</td>";
        result += "<td>姓名</td>";
        result += "<td>公司</td>";
        result += "<td>地址</td>";
        result += "<td>邮编</td>";
        result += "<td>数量</td>";
        result += "<td>级别</td>";
        result += "<td>赠阅人</td>";
        result += "</tr>";
        result += "</thead>";
        string cdate = DateTime.Today.ToString("yyyy-M-d");
        int iNum = 1;
        int iCount = 0;
        //string sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and givebdate<='" + cdate + "' and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' order by givelvl";
        //string sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' order by givelvl";
        string sSql = "";
        string scity = magcity.SelectedValue;
        if (magdept.SelectedValue == "全部")
        {
            switch (scity)
            {
                case "全部":
                    sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' and givelvl='" + maglvl.SelectedValue + "' and uid in (select uid from vw_linkman_key2) order by linkman,company,companyaddr";
                    break;
                case "北京":
                    sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' and givelvl='" + maglvl.SelectedValue + "' and uid in (select uid from vw_linkman_key2) and left(companypostcode,2)='10' order by linkman,company,companyaddr";
                    break;
                case "非北京":
                    sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' and givelvl='" + maglvl.SelectedValue + "' and uid in (select uid from vw_linkman_key2) and left(companypostcode,2)<>'10' order by linkman,company,companyaddr";
                    break;
                default:
                    sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' and givelvl='" + maglvl.SelectedValue + "' and uid in (select uid from vw_linkman_key2) order by linkman,company,companyaddr";
                    break;
            }
        }
        else
        {
            string stempdept = magdept.SelectedValue;
            char[] parschar ={ '=' };
            string[] depts = stempdept.Split(parschar);

            switch (scity)
            {
                case "全部":
                    sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' and givelvl='" + maglvl.SelectedValue + "' and uid in (select uid from vw_linkman_key2) and inman in (select uid from t_user where mycompany='" + depts[0] + "' and mydept='" + depts[1] + "') order by linkman,company,companyaddr";
                    break;
                case "北京":
                    sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' and givelvl='" + maglvl.SelectedValue + "' and uid in (select uid from vw_linkman_key2) and inman in (select uid from t_user where mycompany='" + depts[0] + "' and mydept='" + depts[1] + "') and left(companypostcode,2)='10' order by linkman,company,companyaddr";
                    break;
                case "非北京":
                    sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' and givelvl='" + maglvl.SelectedValue + "' and uid in (select uid from vw_linkman_key2) and inman in (select uid from t_user where mycompany='" + depts[0] + "' and mydept='" + depts[1] + "') and left(companypostcode,2)<>'10' order by linkman,company,companyaddr";
                    break;
                default:
                    sSql = "select linkman,company,companyaddr,companypostcode,giveqty,givelvl,dbo.getbignamebyid(inman) as giveman from vw_givelist where status=4 and giveedate>='" + cdate + "' and givemag like '%" + magname.SelectedValue + "%' and givelvl='" + maglvl.SelectedValue + "' and uid in (select uid from vw_linkman_key2) and inman in (select uid from t_user where mycompany='" + depts[0] + "' and mydept='" + depts[1] + "') order by linkman,company,companyaddr";
                    break;
            }
        }
        TSqlDBReader oList = new TSqlDBReader(sSql);
        result += "<tbody>";
        while (oList.FDataReader.Read())
        {
            result += "<tr>";
            result += "<td>" + iNum.ToString() + "</td>";
            result += "<td>" + oList.FDataReader.GetValue(0).ToString() + "</td>";
            result += "<td>" + oList.FDataReader.GetValue(1).ToString() + "</td>";
            result += "<td>" + oList.FDataReader.GetValue(2).ToString() + "</td>";
            result += "<td>" + oList.FDataReader.GetValue(3).ToString() + "</td>";
            result += "<td>" + oList.FDataReader.GetValue(4).ToString() + "</td>";
            result += "<td>" + oList.FDataReader.GetValue(5).ToString() + "</td>";
            result += "<td>" + oList.FDataReader.GetValue(6).ToString() + "</td>";
            result += "</tr>";
            iCount += Convert.ToInt32(oList.FDataReader.GetValue(4).ToString());
            
            iNum++;
        }
        result += "<tr style=\"background-color:#f5f5f5;\">";
        result += "<td>&nbsp;</td>";
        result += "<td>&nbsp;</td>";
        result += "<td>&nbsp;</td>";
        result += "<td>&nbsp;</td>";
        result += "<td>合计</td>";
        result += "<td>"+iCount.ToString()+"</td>";
        result += "<td>&nbsp;</td>";
        result += "<td>&nbsp;</td>";
        result += "</tr>";
        result += "</tbody>";
        oList.Free();
        result += "</table>";

        manlist.InnerHtml = result;
    }
}
